package pers.feng1240.blog.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import pers.feng1240.blog.entities.Theme;
import pers.feng1240.blog.service.ThemeService;
import pers.feng1240.blog.util.FileUpload;
import pers.feng1240.blog.util.PageHelper;

/*
 * 主题controller
 * 2016-11-13 wc
 */

@Controller
public class ThemeController {

	@Resource
	private ThemeService themeService;	//主题业务逻辑
	/*
	 * 添加主题
	 * @param theme 主题对象
	 * @return
	 */
	@RequestMapping("admin/addTheme")
	public String addTheme(@RequestParam(value = "file", required = false) MultipartFile file ,Theme theme,HttpServletRequest request){
       // theme.setThemePic(imgRelDir+"/"+imgName); //保存相对路径
		theme.setThemePic(FileUpload.uploadPic(file, request));
        themeService.addObject(theme);
		return "redirect:getThemeList.do";
	}
	
	/*
	 * 分页列表
	 * @param pageIndex 要获取的页码
	 * @request map 中  objectList对象列表  pageIndex 要获取的页码  pageSize 页数
	 */
	@RequestMapping("admin/getThemeList")
	public String getThemeList(String pageIndex,HttpServletRequest request){	
		request.setAttribute("map", themeService.getPageList(PageHelper.pageIndex(pageIndex)));
		request.setAttribute("action","getThemeList.do" ); //设置分页动态action
		return "forward:theme.jsp";
	}
	
	/*
	 * 获取所有主题 json
	 */
	@RequestMapping("admin/getThemeJson")
	@ResponseBody
	public List<Object> getThemeJson(){
		return themeService.getObjectList();
	}
	
	/*
	 * 根据id获取主题
	 * @param id 主题id
	 */
	@RequestMapping("admin/getThemeById")
	@ResponseBody
	public Object getThemeById(int id,HttpServletRequest request){
		return themeService.getById(id);
	}
	
	/*
	 * 修改
	 * @param theme 主题对象
	 */
	@RequestMapping("admin/updateTheme")
	public String updateTheme(@RequestParam(value = "file", required = false) MultipartFile file ,Theme theme,HttpServletRequest request){
		theme.setThemePic(FileUpload.uploadPic(file, request));
		themeService.updateObject(theme);
		return "redirect:getThemeList.do";
	}
	
	/*
	 * 删除
	 */
	@RequestMapping("admin/deleteTheme")
	public String deleteOnject(int id){
		themeService.deleteById(id);
		return "redirect:getThemeList.do";
	}
}
